package prog.model;


import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author MrKhoai
 */
public class DataFilter {
    public static ArrayList<Data> dataFiltering(Project pj, ArrayList<Data> srcList) {
        String partern = pj.getPartern();
        int ops = pj.getdownOp();
        if(ops == 1) {

            ArrayList<Integer> stepdown = new ArrayList<Integer>();
            
            for (int j = 0; j < srcList.size(); j++) {
                stepdown.add(-1);
            }
            while (partern.indexOf("*") != -1) {
                String temp = partern.substring(0, partern.indexOf("*"));
                partern = partern.substring(partern.indexOf("*") + 1);

                for (int i = 0; i < srcList.size(); i++) {
                    //kiem tra co chuoi temp trong list khong neu khong thi xoa
                    if (srcList.get(i).getURL().indexOf(temp) == -1) {                                 //su ly lay getURL
                        srcList.remove(i);
                        stepdown.remove(i);
                        i--;
                    } //neu ton tai chuoi temp trong list thi kiem tra tiep da kiem tra chuoi nay lan nao chua
                    else {
                        String avg = srcList.get(i).getURL().substring(stepdown.get(i) + 1);

                        if (stepdown.get(i) < (stepdown.get(i) + 1 + avg.indexOf(temp))) {
                            stepdown.set(i, stepdown.get(i) + avg.indexOf(temp) + temp.length());//da sua

                        } else {

                            srcList.remove(i);
                            stepdown.remove(i);
                            i--;
                        }
                        }
                }
            }
            if (partern.length() > 0) {
                for (int i = 0; i < srcList.size(); i++) {

                    if (srcList.get(i).getURL().indexOf(partern) == -1) {
                        srcList.remove(i);
                        stepdown.remove(i);
                        i--;
                    } //neu ton tai chuoi temp trong list thi kiem tra tiep da kiem tra chuoi nay lan nao chua
                    else {

                        String avg = srcList.get(i).getURL().substring(stepdown.get(i) + 1);

                        if (stepdown.get(i) < (stepdown.get(i) + 1 + avg.indexOf(partern))) {

                            stepdown.set(i, stepdown.get(i) + avg.indexOf(partern) + partern.length());//da sua

                            if (stepdown.get(i) + 1 < srcList.get(i).getURL().length()) {

                                srcList.remove(i);
                                stepdown.remove(i);
                                i--;
                            }
                        } else {
                            srcList.remove(i);
                            stepdown.remove(i);
                            i--;
                        }
                    }
                }
            }
        }
        if(ops == 2) {

            ArrayList<Integer> flagnotdown = new ArrayList<Integer>();
            ArrayList<Integer> stepnotdown = new ArrayList<Integer>();

            for (int j = 0; j < srcList.size(); j++) {
                flagnotdown.add(-1);
                stepnotdown.add(-1);
            }
            while (partern.indexOf("*") != -1) {
                String temp = partern.substring(0, partern.indexOf("*"));
                partern = partern.substring(partern.indexOf("*") + 1);

                for (int i = 0; i < srcList.size(); i++) {
                    //kiem tra co chuoi temp trong list khong neu khong thi xoa
                    if (srcList.get(i).getURL().indexOf(temp) == -1) {
                        flagnotdown.set(i, 1);
                    } //neu ton tai chuoi temp trong list thi kiem tra tiep da kiem tra chuoi nay lan nao chua
                    else {
                        if (flagnotdown.get(i) != 1) {
                            String avg = srcList.get(i).getURL().substring(stepnotdown.get(i) + 1);

                            if (stepnotdown.get(i) < (stepnotdown.get(i) + 1 + avg.indexOf(temp))) {
                                stepnotdown.set(i, stepnotdown.get(i) + avg.indexOf(temp) + temp.length());
                                flagnotdown.set(i, 0);

                            } else {

                                flagnotdown.set(i, 1);
                            }
                        }
                    }
                }
            }

            if (partern.length() > 0) {

                for (int i = 0; i < srcList.size(); i++) {

                    if (srcList.get(i).getURL().indexOf(partern) == -1) {   // neu tim khong thay thi down

                        flagnotdown.set(i, 1);
                    } //neu ton tai chuoi temp trong list thi kiem tra tiep da kiem tra chuoi nay lan nao chua
                    else {
                        if (flagnotdown.get(i) != 1) {
                            String avg = srcList.get(i).getURL().substring(stepnotdown.get(i) + 1);
                            if (stepnotdown.get(i) < (stepnotdown.get(i) + 1 + avg.indexOf(partern))) {          // kiem tra chuoi notdown da ton tai truoc do' chua
                                stepnotdown.set(i, stepnotdown.get(i) + avg.indexOf(partern) + partern.length());
                                if (stepnotdown.get(i) + 1 == srcList.get(i).getURL().length()) {
                                    srcList.remove(i);
                                    flagnotdown.remove(i);
                                    stepnotdown.remove(i);
                                    i--;
                                }
                            } else {
                                flagnotdown.set(i, 1);
                            }
                        }
                    }
                }
            } else {
                for (int i = 0; i < srcList.size(); i++) {
                    if (flagnotdown.get(i) == 0) {
                        srcList.remove(i);
                        flagnotdown.remove(i);
                        stepnotdown.remove(i);
                        i--;
                    }
                }
            }
        }
        return srcList;
    }
}
